# _*_ coding : utf-8 _*_
# @Time : 2023/12/12 15:38
# @Author : 韩宗森
# @File : 31_requests_古诗文
# @Project : pythonProject

import requests
from bs4 import BeautifulSoup

url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
}

response = requests.get(url=url, headers=headers)
content = response.text

# 解析页面源码 然后获取视图状态
soup = BeautifulSoup(content, 'lxml')

# 然后获取_VIEWSTATE
viewstate = soup.select('#__VIEWSTATE')[0].attrs.get('value')

viewstategenerator = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')

# 获取验证码图片  https://so.gushiwen.cn/RandCode.ashx?t=1702367908332
code = soup.select('#imgCode')[0].attrs.get('src')

code_url = 'https://so.gushiwen.cn' + code

# requests里面有一个方法 session() 通过session的返回值就能使用请求变成一个对象
session = requests.session()
# 验证码地址
response_code = session.get(code_url)

# 注意此时要使用二进制数据因为我们要使用的是图片的下载
content_code = response_code.content

# wb的模式就是将二进制数据写入到文件
with open('code.jpg', 'wb') as fp:
    fp.write(content_code)

# 获取了验证码的图片之后下载到本地然后观察验证码观察之后然后在控制台输入这个验证码就可以将这个值给
# code的参数就可以登陆
code_name = input('请输入验证码')
"""
https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx

__VIEWSTATE: uiDXMNA9a8rOhRkPcPNhKylHOEgGBUfsizn2npjdVdQ01CCrcLpzAKk7e8TlD0vB2p9gs8Q3bl0FW/6JVy+o2vRI4frwJXHwtjVJ4F4Nh6411xlrYgr5ONDsNysdYRHVJ3FgOxLTDfgnW63c7n51kIyhXJo=
__VIEWSTATEGENERATOR: C93BE1AE
from: http://so.gushiwen.cn/user/collect.aspx
email: mnmiaoyi@126.com
pwd: 11111111
code: c6vd
denglu: 登录
"""

# 点击登录
url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'

data_post = {
    '__VIEWSTATE': viewstate,
    '__VIEWSTATEGENERATOR': viewstategenerator,
    'from': 'http://so.gushiwen.cn/user/collect.aspx',
    'email': '595165358@qq.com',
    'pwd': 'action',
    'code': code_name,
    'denglu': '登录'
}

response_post = session.post(url=url, headers=headers, data=data_post)

content_post = response_post.text

# 下载
with open('gushiwen.html', 'w', encoding='utf-8') as fp:
    fp.write(content_post)
